package com.iqiu.cms.web;

import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.file.FileService;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.ConvertUtil;
import com.ibeetl.admin.core.util.ValidateConfig;
import com.ibeetl.admin.core.web.JsonResult;
import com.iqiu.cms.entity.CoreTestdata;
import com.iqiu.cms.service.CoreTestdataService;
import com.iqiu.cms.util.EncryptUtil;
import com.iqiu.cms.util.ZXingCode;
import com.iqiu.cms.web.query.CoreTestdataQuery;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * 检测数据 接口
 */
@Controller
public class CoreTestdataController{

    private final Log log = LogFactory.getLog(this.getClass());
    private static final String MODEL = "/cms/coreTestdata";


    @Autowired private CoreTestdataService coreTestdataService;

    @Autowired
    private CorePlatformService platFormService;
    @Autowired
    FileService fileService;
    /* 页面 */

    @GetMapping(MODEL + "/index.do")
    @Function("cms.coreTestdata.query")
    @ResponseBody
    public ModelAndView index() {
        ModelAndView view = new ModelAndView("/cms/coreTestdata/index.html") ;
        view.addObject("search", CoreTestdataQuery.class.getName());
        return view;
    }

    @GetMapping(MODEL + "/edit.do")
    @Function("cms.coreTestdata.edit")
    @ResponseBody
    public ModelAndView edit(Integer id) {
        ModelAndView view = new ModelAndView("/cms/coreTestdata/edit.html");
        CoreTestdata coreTestdata = coreTestdataService.queryById(id);
        view.addObject("coreTestdata", coreTestdata);
        return view;
    }

    @GetMapping(MODEL + "/detail.do")
    @ResponseBody
    public ModelAndView detail(Long id) {
        ModelAndView view = new ModelAndView("/cms/coreTestdata/detail.html");
        CoreTestdata coreTestdata = coreTestdataService.queryById( id);
        view.addObject("detail", coreTestdata);
        return view;
    }

    /* ajax json */

    @PostMapping(MODEL + "/list.json")
    @Function("cms.coreTestdata.query")
    @ResponseBody
    public JsonResult<PageQuery> list(CoreTestdataQuery condtion)
    {
        PageQuery page = condtion.getPageQuery();
        coreTestdataService.queryByCondition(page);
        return JsonResult.success(page);
    }

    @PostMapping(MODEL + "/add.json")
    @Function("cms.coreTestdata.add")
    @ResponseBody
    public JsonResult add(@Validated(ValidateConfig.ADD.class)CoreTestdata coreTestdata)
    {
        coreTestdata.setDelFlag(0);
        coreTestdata.setCreateTime(new Date());
        coreTestdata.setCreateUser(platFormService.getCurrentUser().getId().intValue());
        coreTestdataService.save(coreTestdata);
        return new JsonResult().success();
    }

    @PostMapping(MODEL + "/edit.json")
    @Function("cms.coreTestdata.edit")
    @ResponseBody
    public JsonResult<String> update(@Validated(ValidateConfig.UPDATE.class)  CoreTestdata coreTestdata) {
        boolean success = coreTestdataService.updateTemplate(coreTestdata);
        if (success) {
            return new JsonResult().success();
        } else {
            return JsonResult.failMessage("保存失败");
        }
    }
    @PostMapping(MODEL + "/approval.json")
    @Function("cms.coreTestdata.approval")
    @ResponseBody
    public JsonResult<String> approval(CoreTestdata coreTestdata) {
        coreTestdata.setApprovlId(platFormService.getCurrentUser().getId().intValue());
        boolean success = coreTestdataService.updateTemplate(coreTestdata);
        if (success) {
            return new JsonResult().success();
        } else {
            return JsonResult.failMessage("保存失败");
        }
    }

   
    @GetMapping(MODEL + "/view.json")
    @Function("cms.coreTestdata.query")
    @ResponseBody
    public JsonResult<CoreTestdata> queryInfo(Integer id) {
        CoreTestdata coreTestdata = coreTestdataService.queryById( id);
        return  JsonResult.success(coreTestdata);
    }
    @RequestMapping(MODEL + "/qrCode.json")
    @ResponseBody
    public ModelAndView qrCode(String id, String name, HttpServletResponse response) throws IOException {
        OutputStream os = response.getOutputStream();
        try {
            response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(name+".png","UTF-8"));
            ZXingCode.getLogoQRCode(EncryptUtil.getInstance().AESencode(id,"iqiu@321"), name,os);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    @PostMapping(MODEL + "/delete.json")
    @Function("cms.coreTestdata.delete")
    @ResponseBody
    public JsonResult delete(String ids) {
        if (ids.endsWith(",")) {
            ids = StringUtils.substringBeforeLast(ids, ",");
        }
        List<Long> idList = ConvertUtil.str2longs(ids);
        coreTestdataService.batchDelCoreTestdata(idList);
        return new JsonResult().success();
    }
    

}
